
import threading
import time
import sys
from core.state import MonitorType
##sys.path.append("..")
from core import LOGGER,callback,event,runningEvent
import monitor
import monitor.uip

###采集管理器，林恩德  2022.6.18
# 实现电流、电压、温度等采集管理
class MonitorManager(threading.Thread):
    def __init__(self, manager=None):#group: None = ..., target: Callable[..., Any] | None = ..., name: str | None = ..., args: Iterable[Any] = ..., kwargs: Mapping[str, Any] | None = ..., *, daemon: bool | None = ...) -> None:
        super().__init__(name="CollectThread")#group, target, name, args, kwargs, daemon=daemon)
        self.uipCollect =  monitor.uip.UIPCollect()
        self.batteryCollect = monitor.uip.BatteryStateCollect()
    def start(self):
        LOGGER.info("监测服务启动中")
        super(MonitorManager,self).start()
        pass
    def run(self):
        LOGGER.info("监测服务运行中")
        while(runningEvent.isSet()==False):
            result = self.uipCollect.collect()
            LOGGER.debug("UIP采集{}".format(result))
            callback.getInstance().publish(self,event.MONITOR_DATA_CHANGED,dict(type=MonitorType.CHARGE_STATE,data=result[0]))
            callback.getInstance().publish(self,event.MONITOR_DATA_CHANGED,dict(type=MonitorType.DISCHARGE_STATE,data=result[1]))
            result = self.batteryCollect.collect()
            LOGGER.debug("电源采集{}".format(result))
            callback.getInstance().publish(self,event.MONITOR_DATA_CHANGED,dict(type=MonitorType.BATTERY_STATE,data=result))
            time.sleep(0.1)
        LOGGER.info("ModbudTCP服务终止运行")
if __name__=="__main__":
    monitor = MonitorManager(None)
    monitor.start()
    time.sleep(100)